package cn.one.controller;

import cn.one.common.DtoUtil;
import cn.one.dto.Dto;
import cn.one.pojo.User;
import cn.one.service.user.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Api(value = "API")
public class UserController extends BaseController{
    @Resource
    private UserServiceImpl userService;
    //测试Map<String,String>也可以在页面显示json数据
    @RequestMapping(value = "/index",produces = "application/json",method = RequestMethod.GET)
    @ResponseBody
    public User test(){
       /* Map param = new HashMap();
        param.put("parentId", 16);*/
       /*List<String> l = new ArrayList<String>();
       l.add("a");
       l.add("b");*/
       User user = new User();
        user.setPassword("123456");
        user.setWNumber(123);
        return user;
    }
    //通过使用model,在页面显示json数据
    @ApiOperation(value ="通过id获取USER",httpMethod = "GET",protocols = "HTTP",
            produces = "application/json",response = User.class,notes = "Map<String,String>"
            +"<p>对象</p>"+"<p>集合List</p>")
    @RequestMapping(value = "/biz",produces = "application/json",method = RequestMethod.GET)
    @ResponseBody
    public User getUserbyId(@RequestParam String id, Model model){
        User user = new User();
        int i = Integer.parseInt(id);
        try {
            user = userService.getUserById(i);
            model.addAttribute(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    //使用Dto为返回值,直接用Dto对象就不用Model提交到页面
    @RequestMapping(value = "/testDto",produces = "application/json",method = RequestMethod.GET)
    @ResponseBody
    public Dto testDto(@RequestParam String id){
        User user = new User();
        int i = Integer.parseInt(id);
        try {
            user = userService.getUserById(i);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(user);

    }



}
